<?php
/**
 * Example Action Plugin:   Example Component.
 *
 * @author     Samuele Tognini <samuele@cli.di.unipi.it>
 */

if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once DOKU_PLUGIN.'action.php';
include_once DOKU_PLUGIN.'uniaction/utilityfunctions.php';

class action_plugin_uniactionhandler extends DokuWiki_Action_Plugin {

	/**
	 * Register its handlers with the DokuWiki's event controller
	 */
	function register(&$controller) {
		$controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this,
                                   '_hookjs');
	}

	/**
	 * Hook js script into page headers.
	 *
	 * @author Samuele Tognini <samuele@cli.di.unipi.it>
	 */
	function _hookjs(&$event, $param) {
			
		//start internal uni logic

		$_SESSION['uni']['login_message'] = "";
		$_SESSION['uni']['subscribe_message'] = "";
			
		if(!isset($_POST['uni_action']) && !isset($_GET['uni_action']) ) return;
		//check for login / logout etc.
		if($_POST['uni_action'] == "login" ){
			$_SESSION['uni']['login_message'] = tryUniLogin($_POST['uni_email'],$_POST['uni_password']);
		}
		if($_GET['uni_action'] == "subscribe" ){
			$id = intval($_GET['course_id']);
			$_SESSION['uni']['subscribe_message'] = uni_subscribe($id);				
		}
			if($_GET['uni_action'] == "unsubscribe" ){
			$id = intval($_GET['course_id']);
			$_SESSION['uni']['subscribe_message'] = uni_unsubscribe($id);				
		}
		
			

		if($_GET['uni_action'] == "logout" ){
			unset($_SESSION['uni']);
		}
			
	}
}

/**
 * 
 * subscribe if possible or return error msg
 * @param unknown_type $id
 */
function uni_subscribe($id){
	$id = intval($id);
	//check if logged in 
	if(!isLoggedIn())
		return "Du bist nicht angemeldet!";
	//check if already subscribed
	if(userSubscribedToCourse($id))
		return "Du bist bereits eingeschrieben!";
		
		
	//subscribe		
	$userid = uni_getUserID();
		
	//check if there is still space for this one
	if(!uni_userCanJoinCourse($userid, $id)){
		return "Leider keine Plätze mehr frei!";
	}
		
	$db = getDBConnection();
	$query = "insert into hoert set sch_id='$userid',haelt_id='$id'";
	$result = doQuery($query);	
	if($result === FALSE) trigger_error("Datenbankfehler.",E_WARNING);	
	return "Du hast dich erfolgreich angemeldet.";
}

function uni_userCanJoinCourse($userid, $id){
	//TODO CAN MORE JOIN THAN SPACE IS AVAILABLE? 
	//IF YES JUST RETURN TRUE;
	$db = getDBConnection();
	$query = "SELECT is_vorlesung,max_subscriber,subscriber from haelt LEFT JOIN teilnehmer on haelt.id = teilnehmer.id LEFT JOIN veranstaltung on haelt.ver_id = veranstaltung.id WHERE haelt.id ='".intval($id)."'";
	$result = doQuery($query);	
	if($result === FALSE) trigger_error("Datenbankfehler.",E_WARNING);	
	$array = mysql_fetch_row($result);
	if($array === FALSE) trigger_error("Datenbankfehler.@userid: $userid, id: $id",E_WARNING);		
	
	//vorlesung has no space limit
	if($array[0] == 1) return true;
	
	else return $array[1] > $array[2]; 
	
}

function uni_getPaidMoney($userid, $id){
	
	$id = intval($id);
	$userid = intval($userid);
	
	$db = getDBConnection();
	$query = "SELECT paid from hoert WHERE sch_id='$userid' AND haelt_id='$id'";
	$result = doQuery($query);	
	if($result === FALSE) trigger_error("Datenbankfehler.",E_WARNING);	
	$array = mysql_fetch_row($result);
	if($array === FALSE) return 0;	
	else return $array[0];
}
/**
 * 
 * subscribe if possible or return error msg
 * @param unknown_type $id
 */
function uni_unsubscribe($id){
	$id = intval($id);
	//check if logged in 
	if(!isLoggedIn())
		return "Du bist nicht angemeldet!";
	//check if already subscribed
	if(!userSubscribedToCourse($id))
		return "Du bist bereits ausgetragenn!";
		
	$userid = uni_getUserID();
	//check if user already paid money 
	if(uni_getPaidMoney($userid, $id) > 0)
		return "Du hast bereits Geld überwiesen, zum Abmelden nimm bitte Kontakt mit dem Sommeruni Team auf!";
		
	//unsubscribe		
	$db = getDBConnection();
	$query = "DELETE FROM hoert WHERE sch_id='$userid' AND haelt_id='$id'";
	$result = doQuery($query);	
	if($result === FALSE) trigger_error("Datenbankfehler.",E_WARNING);	
	return "Du hast dich erfolgreich abgemeldet.";
}
?>